package com.topboya.linker.ui.activity;

import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.topboya.linker.AppConst;
import com.topboya.linker.R;
import com.topboya.linker.diyview.LetterSpacingTextView;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.presenter.LoginAtPresenter;
import com.topboya.linker.utils.CacheUtils;
import com.topboya.linker.utils.LogUtils;

import java.util.ArrayList;
import java.util.List;

public class GuideAcitvity extends BaseActivity{

    private static final String TAG = "GuideAcitvity";
    private ViewPager vp_guide;
    private Button btn_experience;
    private View guideview1, guideview2, guideview3;
    private List<View> viewList;//view数组
    private ImageView img_indiactor1,img_indiactor2,img_indiactor3;
    private LinearLayout ll_indiactor;

    @Override
    protected int getLayoutId() {

        return R.layout.activity_guide;
    }

    @Override
    protected void initView() {

        vp_guide = findViewById(R.id.vp_guide);
        btn_experience = findViewById(R.id.btn_experience);
        ll_indiactor = findViewById(R.id.ll_indiactor);
        img_indiactor1 = findViewById(R.id.img_indiactor1);
        img_indiactor2 = findViewById(R.id.img_indiactor2);
        img_indiactor3 = findViewById(R.id.img_indiactor3);

        img_indiactor1.setBackground(getResources().getDrawable(R.drawable.guide_selected));
        img_indiactor2.setBackground(getResources().getDrawable(R.drawable.guide_no_selected));
        img_indiactor3.setBackground(getResources().getDrawable(R.drawable.guide_no_selected));

    }

    @Override
    protected void initData() {

        viewList = new ArrayList<>();
        LayoutInflater inflater=getLayoutInflater();
        guideview1 = inflater.inflate(R.layout.guide_view_item, null);
        guideview2 = inflater.inflate(R.layout.guide_view_item,null);
        guideview3 = inflater.inflate(R.layout.guide_view_item, null);

        LetterSpacingTextView guide1_tv_title = guideview1.findViewById(R.id.tv_title);
        LetterSpacingTextView guide1_tv_tips = guideview1.findViewById(R.id.tv_tips);
        RelativeLayout rl_guide = guideview1.findViewById(R.id.rl_guide);
        rl_guide.setBackground(getResources().getDrawable(R.drawable.guide1));
        guide1_tv_title.setText("[创建任务]");
        guide1_tv_tips.setText("明确工作计划目标");
        guide1_tv_title.setSpacing(10);
        guide1_tv_tips.setSpacing(10);

        LetterSpacingTextView guide2_tv_title = guideview2.findViewById(R.id.tv_title);
        LetterSpacingTextView guide2_tv_tips = guideview2.findViewById(R.id.tv_tips);
        RelativeLayout rl_guide2 = guideview2.findViewById(R.id.rl_guide);
        rl_guide2.setBackground(getResources().getDrawable(R.drawable.guide2));
        guide2_tv_title.setText("[手机定位]");
        guide2_tv_tips.setText("精确找到拜访终端");
        guide2_tv_title.setSpacing(10);
        guide2_tv_tips.setSpacing(10);


        LetterSpacingTextView guide3_tv_title = guideview3.findViewById(R.id.tv_title);
        LetterSpacingTextView guide3_tv_tips = guideview3.findViewById(R.id.tv_tips);
        RelativeLayout rl_guide3 = guideview3.findViewById(R.id.rl_guide);
        rl_guide3.setBackground(getResources().getDrawable(R.drawable.guide3));
        guide3_tv_title.setText("[信息查询]");
        guide3_tv_tips.setText("实时了解授权信息");
        guide3_tv_title.setSpacing(10);
        guide3_tv_tips.setSpacing(10);

        viewList.add(guideview1);
        viewList.add(guideview2);
        viewList.add(guideview3);
        vp_guide.setAdapter(new GuideAdapter());
    }

    @Override
    protected void initListener() {

        btn_experience.setOnClickListener(v -> {

            CacheUtils.putBoolean(GuideAcitvity.this,AppConst.ISMAIN,false);
            Intent intent = new Intent(GuideAcitvity.this,LoginActivity.class);
            startActivity(intent);
            finish();

        });
        vp_guide.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                if (position ==2){

                    btn_experience.setVisibility(View.VISIBLE);
                    ll_indiactor.setVisibility(View.GONE);
                }else {

                    btn_experience.setVisibility(View.GONE);
                    ll_indiactor.setVisibility(View.VISIBLE);
                    if (position==0){

                        img_indiactor1.setBackground(getResources().getDrawable(R.drawable.guide_selected));
                        img_indiactor2.setBackground(getResources().getDrawable(R.drawable.guide_no_selected));
                        img_indiactor3.setBackground(getResources().getDrawable(R.drawable.guide_no_selected));
                    }else if (position ==1){

                        img_indiactor1.setBackground(getResources().getDrawable(R.drawable.guide_no_selected));
                        img_indiactor2.setBackground(getResources().getDrawable(R.drawable.guide_selected));
                        img_indiactor3.setBackground(getResources().getDrawable(R.drawable.guide_no_selected));
                    }
                }


            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected LoginAtPresenter createPresenter() {

        return null;
    }



    public class GuideAdapter extends PagerAdapter {

        @Override
        public int getCount() {

            LogUtils.v(TAG +"size:"+viewList.size());
            return viewList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {

            return view ==object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {

            container.addView(viewList.get(position));
            return viewList.get(position);


        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {

            RelativeLayout relativeLayout=(RelativeLayout)object;
            container.removeView(relativeLayout);

        }
    }
}
